#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import os
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()

# 隐式等待
driver.implicitly_wait(5)

# 获取html绝对路径
file_path = os.path.join('file:///', os.getcwd(), "selenium.html")
print(file_path)

# 打开html文件
driver.get(file_path)

#最大化窗口
driver.maximize_window()

# 定位元素
username_ele = driver.find_element_by_css_selector(
    'div#signup input[name="username"]')
password_ele = driver.find_element_by_css_selector(
    'div#signup input[name="password"]')
city_ele = driver.find_element_by_css_selector(
    'div#signup select[name="city"]'
)

sign_form = driver.find_element_by_css_selector('div#signup form')

# 操作元素
username_ele.send_keys("rabbit")


password_ele.send_keys("love")

Select(city_ele).select_by_value('sz')

sign_form.submit()
# 强制等待
sleep(0.1)

# 显式等待,超时时间10秒，每秒查找一次
success_elem = WebDriverWait(driver, 10, 1).until(
    lambda x: x.find_element_by_css_selector("div#signup p").text == "注册成功")


driver.close()

driver.quit()
